package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SaveSensitiveDto;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-09-11 14:23
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive>
        implements WmSensitiveService {

    @Override
    @Transactional //默认只能回滚runtimeException,让所有异常回滚
    public void saveSensitive(String sensitives){
        //- 2.已存在的敏感词，不能保存 select * from xxx where sensitives = #{sensitives}
//        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        checkSensitiveIsExist(sensitives);
        //- 3.保存
        WmSensitive wmSensitive1 = new WmSensitive();
        wmSensitive1.setSensitives(sensitives);
        wmSensitive1.setCreatedTime(new Date());
        save(wmSensitive1);

    }


    @Override
    public PageResponseResult findPage(SensitiveDto sensitiveDto) {

        //1.构建查询参数
//        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = Wrappers.<WmSensitive>lambdaQuery();
//        //select * from xxx where sensitives like '%冰%'
//        if(StringUtils.isNotBlank(sensitiveDto.getName())){
//            lambdaQueryWrapper.like(WmSensitive::getSensitives,sensitiveDto.getName());
//        }

        //like第一参数判断like条件是否需要添加true添加 false不添加
        LambdaQueryWrapper<WmSensitive> query = Wrappers.<WmSensitive>lambdaQuery().like(StringUtils.isNotBlank(sensitiveDto.getName()),
                WmSensitive::getSensitives, sensitiveDto.getName());

        //2.分页参数
        Page<WmSensitive> p = new Page<>(sensitiveDto.getPage(), sensitiveDto.getSize());

        //3.查询
        Page<WmSensitive> pageResult = page(p, query);

        //4.封装返回结果
        PageResponseResult pageResponseResult =
                new PageResponseResult((int) pageResult.getCurrent(), (int) pageResult.getSize(), (int) pageResult.getTotal());
        pageResponseResult.setData(pageResult.getRecords());
        return pageResponseResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSensitive(SaveSensitiveDto dto) {

        checkSensitiveIsExist(dto.getSensitives());
        //update xxx set sensitives = '测试' where id = 1
        boolean update = update(Wrappers.<WmSensitive>lambdaUpdate()
                .eq(WmSensitive::getId, dto.getId())
                .set(WmSensitive::getSensitives, dto.getSensitives())
        );

        if(!update){
            //如果更新失败代表id不存在
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

    }

    /**
     * 判断敏感词在数据库中是否存在
     * @param sensitive
     */
    private void checkSensitiveIsExist(String sensitive) {
        int count = count(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, sensitive));
        if(count > 0){
            //存在
            throw new CustomException(AppHttpCodeEnum.DATA_EXIST);
        }
    }
}
